package cn.app.edu.projectpracticeofpracticaltraining.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.app.edu.projectpracticeofpracticaltraining.R;
import cn.app.edu.projectpracticeofpracticaltraining.entity.ArticleEntity;

public class HomeFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<ArticleEntity.DataBean.DatasBean> mList;
    private View mBannerView;
    private static final int ARTICLE = 0;
    private static final int BANNER = 1;
    private OnClickListener mOnClickListener;

    public HomeFragmentAdapter(Context context, List<ArticleEntity.DataBean.DatasBean> list,View bannerView,OnClickListener onClickListener) {
        this.context = context;
        this.mList = list;
        this.mBannerView = bannerView;
        this.mOnClickListener = onClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0) {
            return BANNER;
        }else{
            return ARTICLE;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.recycler_item_home_frament, parent,false);
        switch (viewType) {
            case ARTICLE:
                return new viewHolder(inflate);
            case BANNER:
                return new BannerViewHolder(mBannerView);
            default:
                return null;
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holders, int position) {


        if (getItemViewType(position)==ARTICLE) {
            final ArticleEntity.DataBean.DatasBean datasBean = mList.get(position-1);

            final viewHolder holder = (viewHolder) holders;
            holder.tvTitle.setText(datasBean.getTitle());
            holder.tvTime.setText(datasBean.getNiceDate());
            holder.tvShare.setText(datasBean.getShareUser());
            holder.tvSort.setText(datasBean.getSuperChapterName()+"/"+datasBean.getChapterName());
            if (mOnClickListener!=null) {
                holder.mItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnClickListener.setOnClickListener(v,holder.getAdapterPosition(), datasBean);
                    }
                });
            }
            if (datasBean.isFresh()) {
                holder.ivNeworold.setImageResource(R.mipmap.new_icon);
            }else {
                holder.ivNeworold.setImageResource(R.mipmap.old_icon);
            }
        }

    }


    @Override
    public int getItemCount() {
        return mList.size()+1;
    }

    static class viewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvSort;
        TextView tvShare;
        TextView tvTime;
        ImageView ivNeworold;
        LinearLayout mItem;

        public viewHolder(View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tv_time_recycler_item_home_fragment);
            tvTitle=itemView.findViewById(R.id.tv_title_recycler_item_home_fragment);
            tvSort=itemView.findViewById(R.id.tv_sort_recycler_item_home_fragment);
            tvShare=itemView.findViewById(R.id.tv_share_recycler_item_home_fragment);
            ivNeworold=itemView.findViewById(R.id.iv_neworold_recycler_item_home_fragment);
            mItem = itemView.findViewById(R.id.ly_item_recycler_home_fragment);
        }
    }

    static class BannerViewHolder extends RecyclerView.ViewHolder{

        public BannerViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface OnClickListener{
        void setOnClickListener(View view,int position,ArticleEntity.DataBean.DatasBean datasBean);
    }


}
